开始
安装
npm install peerjs
简单示例
import { Peer } from "peerjs";
const peer = new Peer("pick-an-id");
连接
const conn = peer.connect("another-peers-id");
conn.on("open", () => {
conn.send("hi!");
});
收到
peer.on("connection", (conn) => {
conn.on("data", (data) => {
// Will print 'hi!'
console.log(data);
});
conn.on("open", () => {
conn.send("hello!");
});
});